package com.xzj.admin.resource.sync.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import com.xzj.common.datasource.domain.PageReq;
import cn.hutool.core.util.ObjectUtil;
import com.xzj.common.datasource.domain.PageResp;
import com.xzj.admin.resource.sync.domain.entity.ResMessageSource;
import com.xzj.admin.resource.sync.service.ResMessageSourceService;

import java.util.List;

/**
 * @program: xzj-admin
 * @ClassName: ResMessageSourceController
 * @description: 资源消息源;(res_message_source)表控制层
 * @author: XiaZunJun
 * @create: 2024-10-31
 */
@Tag(name = "资源消息源对象功能接口")
@RestController
@RequestMapping("/resMessageSource")
public class ResMessageSourceController{
    @Resource
    private ResMessageSourceService resMessageSourceService;

    /**
     * 通过ID查询单条数据
     *
     * @param sourceId 主键
     * @return 实例对象
     */
    @Operation(summary = "通过ID查询单条数据")
    @GetMapping("/queryById/{sourceId}")
    public ResMessageSource queryById(@PathVariable(name = "sourceId") String sourceId){
        return resMessageSourceService.queryById(sourceId);
    }

    /**
     * 分页查询
     *
     * @param pageReq 分页筛选条件对象
     * @return 查询结果
     */
    @Operation(summary = "分页查询")
    @PostMapping("/pageQuery")
    public PageResp<ResMessageSource> pageQuery(@RequestBody PageReq<ResMessageSource> pageReq){
        return resMessageSourceService.pageQuery(pageReq);
    }

    @Operation(summary = "查询对应云类型下得所有消息源")
    @PostMapping("/queryAllByCloud")
    public List<ResMessageSource> queryAllByCloud(@RequestBody ResMessageSource resMessageSource){
        return resMessageSourceService.queryAllByCloud(resMessageSource);
    }

    /**
     * 新增数据
     *
     * @param resMessageSource 实例对象
     * @return 实例对象
     */
    @Operation(summary ="新增Or更新数据")
    @PostMapping("/addOrUpdate")
    public ResMessageSource addOrUpdate(@RequestBody ResMessageSource resMessageSource){
        if(ObjectUtil.isNotEmpty(resMessageSource.getSourceId())){
            resMessageSourceService.update(resMessageSource);
        }else{
            resMessageSourceService.insert(resMessageSource);
        }
        return resMessageSource;
    }

    /**
     * 通过主键删除数据
     *
     * @param sourceId 主键
     * @return 是否成功
     */
    @Operation(summary ="通过主键删除数据")
    @GetMapping("/deleteById/{sourceId}")
    public boolean deleteById(@PathVariable(name = "sourceId") String sourceId){
        return resMessageSourceService.deleteById(sourceId);
    }
}